Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "lexical context" information to the macro expansion context #1554

Merged
merged 2 commits into from
Feb 5, 2024

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Apr 17, 2023

The lexical context of a particular macro expansion involves all of the
enclosing contexts, including functions, types and extensions,
properties and subscripts, and so on. The lexical context stack can be
used to, for example, determine which type a macro was expanded
within, or gather the parameters of the enclosing function for an
assertion, etc.

@DougGregor DougGregor requested a review from ahoppen as a code owner April 17, 2023 12:21
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor DougGregor force-pushed the parent-context-of-macro branch from db6a793 to 63606b0 Compare April 17, 2023 12:22
@DougGregor
Copy link
Member Author

@swift-ci please test

Comment on lines 77 to 78
if firstName.text != "_", parameterHasArgumentLabelByDefault
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if firstName.text != "_", parameterHasArgumentLabelByDefault
{
if firstName.text != "_", parameterHasArgumentLabelByDefault {

extension SyntaxProtocol {
/// Form a function name.
private func formFunctionName(
_ baseName: String, _ parameters: ParameterClauseSyntax?,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

baseName could just be a TokenSyntax. Then you could use .init instead of "init" below


// Extensions
if let extensionDecl = self.as(ExtensionDeclSyntax.self) {
// FIXME: It would be nice to be able to switch on type syntax...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could switch over extensionDecl.kind and then get the meta type using SyntaxKind.syntaxNodeType. You could also use that above.

return #""<unknown>""#
}

return ExprSyntax("\(literal: name)").with(\.leadingTrivia, node.leadingTrivia)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably also want to retain the node’s trailing trivia, right?

@@ -769,6 +889,70 @@ final class MacroSystemTests: XCTestCase {
)
}

func testPoundFunction() {
assertMacroExpansion(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to have a separate assertMacroExpansion invocation for each #function. In my experience that makes it a lot easier to
a) check how #function is being transformed in each case and
b) debug the test case if something is going wrong

The lexical context of a particular macro expansion involves all of the
enclosing contexts, including functions, types and extensions,
properties and subscripts, and so on. The lexical context stack can be
used to, for example, determine which type a macro was expanded
within, or gather the parameters of the enclosing function for an
assertion, etc.
@DougGregor DougGregor force-pushed the parent-context-of-macro branch from 63606b0 to 5c47c90 Compare February 3, 2024 00:08
@DougGregor DougGregor requested a review from bnbarham as a code owner February 3, 2024 00:08
@DougGregor DougGregor changed the title [Macros] Add parentContext(of:) operation to MacroExpansionContext. Add "lexical context" information to the macro expansion context Feb 3, 2024
@DougGregor
Copy link
Member Author

swiftlang/swift#71362

@swift-ci please test

/// blocks removed.
///
/// The first entry in the array is the innermost context, which could be
/// the syntax node to which
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also an unfinished comment.

@DougGregor
Copy link
Member Author

swiftlang/swift#71362

@swift-ci please test

@DougGregor
Copy link
Member Author

swiftlang/swift#71362

@swift-ci please test Windows

1 similar comment
@DougGregor
Copy link
Member Author

swiftlang/swift#71362

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

swiftlang/swift#71362

@swift-ci test Windows

@DougGregor DougGregor merged commit 865e3ba into swiftlang:main Feb 5, 2024
3 checks passed
@DougGregor DougGregor deleted the parent-context-of-macro branch February 5, 2024 14:31
grynspan added a commit to swiftlang/swift-testing that referenced this pull request Apr 1, 2024
This PR conditionally adopts the new `lexicalContext` member of
`MacroExpansionContext` added in swiftlang/swift-syntax#1554.
If the SwiftSyntax600 pseudo-module is available, then this member should be
available for use and can be used to perform additional diagnostics for tests
and to get the names of their containing types.

With this PR, if built against an older toolchain (5.11 or earlier), the old
hacky "is there leading whitespace?" mechanism is still used.

A future PR will recursively perform suite-level diagnostics on the lexical
contexts containing tests and suites, so that a test cannot be (easily) inserted
into a type that cannot be used as a suite.

Resolves rdar://109439578.
grynspan added a commit to swiftlang/swift-testing that referenced this pull request Apr 1, 2024
This PR conditionally adopts the new `lexicalContext` member of
`MacroExpansionContext` added in
swiftlang/swift-syntax#1554. If the SwiftSyntax600
pseudo-module is available, then this member should be available for use
and can be used to perform additional diagnostics for tests and to get
the names of their containing types.

With this PR, if built against an older toolchain (5.11 or earlier), the
old hacky "is there leading whitespace?" mechanism is still used.

A future PR will recursively perform suite-level diagnostics on the
lexical contexts containing tests and suites, so that a test cannot be
(easily) inserted into a type that cannot be used as a suite.

Resolves rdar://109439578.

### Checklist:

- [x] Code and documentation should follow the style of the [Style
Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md).
- [x] If public symbols are renamed or modified, DocC references should
be updated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants